package net.dgg.zz.credential.warningConfigure.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.tmd.foundation.platform.annotation.AuthOpt;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.excption.CommonExcption;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DataTableResponse;
import net.dgg.zz.credential.excption.CertificateExcption;
import net.dgg.zz.credential.warningConfigure.dto.WarningConfigureDto;
import net.dgg.zz.credential.warningConfigure.entity.WarningConfigure;
import net.dgg.zz.credential.warningConfigure.service.SystemConfigureService;
import net.fblock.log.NLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @author zsx
 * @Title: CertificateConfigureController
 * @ProjectName zzsc
 * @Description: 证书系统配置
 * @date 2018/8/13 15:32
 */

@Controller
@RequestMapping("/zs/warning_configure")
public class SystemConfigureController extends DggBaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SystemConfigureService configureService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private SessionManager sessionManager;

    /*
     * @Author zsx
     * @Date 2018-08-14 03:08:38
     * @Description  证书初始化页面
     * @return string
     */
    @AuthOpt(code = "ZS_WARNING_CONFIG")
    @RequestMapping("/certificate_config")
    public String certificateConfig(Model model) {
        model.addAttribute("warningType", CredentialConstant.GENERAL_CREDENTIAL);
        return "zzsc/systemConfig/certificate_system_config";
    }

    /*
     * @Author zsx
     * @Date 2018-08-14 03:08:38
     * @Description  资质初始化页面
     * @return string
     */
    @AuthOpt(code = "ZZ_WARNING_CONFIGURE")
    @RequestMapping("/aptitude_config")
    public String aptitudeConfig(Model model) {
        model.addAttribute("warningType", CredentialConstant.ZZ_CREDENTIAL);
        return "zzsc/systemConfig/aptitude_system_config";
    }

    /*
     * @Author zsx
     * @Date 2018-08-13 05:08:40
     * @Description  分页获取证书和资质系统配置
     * @param params
     * @return DataTableResponse
     */
    @RequestMapping("/query_system_config")
    @ResponseBody
    @NLog()
    public DataTableResponse querySystemConfigList(@RequestParam Map<String, Object> params, HttpServletRequest request) {
        try {
            DataTableRequest dt = DataTableUtils.getParam(request);
            params.put("page", dt.getPage());
            params.put("limit", dt.getLength());
            List<WarningConfigure> list = configureService.queryWarningConfigByWithPage(params);
            return new DataTableResponse().data(params, list);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new DataTableResponse().data(params, null);
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-14 11:08:17
     * @Description 添加证书书
     * @param signOrgId 事业部id
     * @param warningType 证书类型
     * @param signOrgName 事业部名称
     * @return DggRestResponse
     */
    @RequestMapping("/add_warning_config")
    @ResponseBody
    @NLog
    public DggRestResponse addWarningConfig(Long signOrgId, String warningType, String signOrgName) {
        try {
            String msg = configureService.addWarningConfig(signOrgId, warningType, signOrgName);
            if (msg.isEmpty()) {
                return this.getSuccessResponse("");
            }
            return this.getFailResponse(msg);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getFailResponse("服务器错误");
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-14 05:08:56
     * @Description  修改系统配置
     * @param configure
     * @return DggRestResponse
     */
    @RequestMapping("/update_warning_config")
    @ResponseBody
    @NLog
    public DggRestResponse updateWarningConfig(WarningConfigureDto configureDto) {
        try {
            Map<String, Object> map = configureService.updateWarningConfig(configureDto);
            if ((Boolean) map.get("status")) {
                return this.getSuccessResponse("");
            } else {
                return this.getFailResponse(map.get("msg").toString());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getFailResponse("服务器错误");
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-16 02:08:21
     * @Description 更新单个预警值
     * @param signOrgId 事业部id(只能登录人有权限，所有直接获取)
     * @param warningType 类型(证书 GENERAL_CENTIFICATE /资质 ZZ_CENTIFICATE)
     * @param warningName 预警名称（对应名称为WarningType 里面code）
     * @param warningValue 预警值
     * @return DggRestResponse
     */
    @RequestMapping("/update_single_warning")
    @ResponseBody
    @NLog
    public DggRestResponse updateSingleWarning(@RequestParam Map<String, Object> params) {
        try {
            params.put("signOrgId", commonService.findUserSyb(sessionManager.getCurrentUser()));
            Map<String, Object> map = configureService.updateSingleWarning(params);
            if ((Boolean) map.get("status")) {
                return this.getSuccessResponse("");
            } else {
                return this.getFailResponse(map.get("msg").toString());
            }
        } catch (CertificateExcption e) {
            return this.getFailResponse(e.getMessage());
        } catch (CommonExcption e) {
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getFailResponse("服务器错误，请联系管理员");
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-17 01:08:09
     * @Description 根据事业部id和类型查询预警值
     * @param params
     * @return DggRestResponse
     */
    @RequestMapping("/query_single_warning")
    @ResponseBody
    @NLog
    public DggRestResponse querySingleWarning(Map<String, Object> params) {
        try {
            Map<String, Object> map = configureService.querySingleWarning(params);
            if ((Boolean) map.get("status")) {
                return this.getSuccessResponse(map.get("configValue"));
            } else {
                return this.getFailResponse(map.get("msg").toString());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getFailResponse("服务器错误");
        }
    }

    /**
     * 设置库容预警页面
     *
     * @param model
     * @return
     */
    @NLog
    @RequestMapping("/set_warning_page")
    public String setWarningPage(Model model) {
        try {
            WarningConfigure warningConfigure = configureService.findConfigureByOrgId(null, CredentialConstant.GENERAL_CREDENTIAL);
            model.addAttribute("warningConfigure", warningConfigure);
            model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
            return "/zzsc/credential/generalCredential/set_warning";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("noSyb", "您所在的组织机构没有所在事业部，不能进行设置");
            return "/zzsc/credential/generalCredential/set_warning";
        }
    }

    /**
     * 设置库容预警页面
     *
     * @param model
     * @return
     */
    @NLog
    @RequestMapping("/set_zz_warning_page")
    public String setZZWarningPage(Model model) {
        try {
            WarningConfigure warningConfigure = configureService.findConfigureByOrgId(null, CredentialConstant.ZZ_CREDENTIAL);
            model.addAttribute("warningConfigure", warningConfigure);
            model.addAttribute("generalType", CredentialConstant.ZZ_CREDENTIAL);
            return "/zzsc/credential/generalCredential/set_warning";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("noSyb", "该用户组织机构没有所在事业部");
            return "/zzsc/credential/generalCredential/set_warning";
        }
    }
}
